package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Collection;
import java.util.Scanner;

/**
 * 程序启动后，要求用户输入标题和正文，然后将对应的数据插入到article表里面
 */
public class Test1 {
    public static void main(String[] args) {
        /*
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入标题：");
        String title = sc.nextLine();
        System.out.println("请输入正文：");
        String content = sc.nextLine();

        try(
            Connection conn= DBUtil.getConnection();
        ){
            // 创建sql语句
            String sql = "INSERT INTO article(title,content)" +
                    "VALUES(?,?)";

            // 创建prepareStatement对象
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,title);
            ps.setString(2,content);
            ps.executeUpdate();

            System.out.println("插入成功");

        }catch (Exception e){
            e.printStackTrace();
        }
*/

        // 创建Article对象
        Article article = InputUtil.getInputObject(new Article(),"文章","文章");
        //

        try(
            Connection conn = DBUtil.getConnection();
        ){
            String sql = "INSERT INTO article(title,content) " +
                    "VALUES(?,?)";

            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setString(1,article.getTitle());
            ps.setString(2,article.getContent());
            ps.executeUpdate();
            System.out.println("插入成功");

        }catch (Exception e){
            e.printStackTrace();
        }




    }
}
